package br.great.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import br.great.beans.Collection_ContentAnnotation;
import br.great.beans.Photo_ContentAnnotation;

/**
 * PhotoMap
 * Copyright (c) 2007-2008 STEAMER - LIG, All rights reserved.
 */
/**
 * @author Developpement of PhotoMap
 */
public class DAO_ContentAnnotation {

    int i;
    private static volatile DAO_ContentAnnotation instaceDAO_ContentAnnotation = new DAO_ContentAnnotation();

    /** The private constructor of DAO_ContentAnnotation for singleton
     */
    private DAO_ContentAnnotation() {
    }

    /** The method for get the instance of the class DAO_ContentAnnotation
     * @return DAO_ContentAnnotation instaceDAO_ContentAnnotation
     */
    public static DAO_ContentAnnotation getInstance() {
        //if(instaceDAO_ContentAnnotation==null)
        synchronized (DAO_ContentAnnotation.class) {
            if (instaceDAO_ContentAnnotation == null) {
                instaceDAO_ContentAnnotation = new DAO_ContentAnnotation();
            }
        }
        return instaceDAO_ContentAnnotation;
    }

    public int insertAnnotation(Collection_ContentAnnotation cAnnotation) {
        String testPAnnotation = "SELECT id_content_annotation FROM content_annotation WHERE Lower(phrase)=Lower('" + cAnnotation.getPHRASE() + "');";
        String insertCAnnotation = "insert into CONTENT_ANNOTATION values(nextval('seqmax_cAnnotation'),'" + cAnnotation.getPHRASE() + "');";
        String getIdCAnnotation = "select max(id_CONTENT_ANNOTATION) from CONTENT_ANNOTATION";

        PgConnect conn = PgConnect.getInstance();
        try {
            ResultSet rs_tp = conn.executeQuery(testPAnnotation);
            if (rs_tp.next()) {
                i = rs_tp.getInt("id_content_annotation");
            } else {
                conn.executeInsert(insertCAnnotation);
                ResultSet rs = conn.executeQuery(getIdCAnnotation);
                while (rs.next()) {
                    i = rs.getInt("max");
                }
            }
        } catch (SQLException ex) {
            ex.getStackTrace();
        }
        conn.Close();
        return i;
    }

    public int insertAnnotation(Photo_ContentAnnotation pAnnotation) {
        //String testPAnnotation = "SELECT id_content_annotation FROM content_annotation WHERE phrase='" + pAnnotation.getPHRASE() + "';";
        String insertPAnnotation = "INSERT INTO CONTENT_ANNOTATION values(nextval('seqmax_cAnnotation'),'" + pAnnotation.getPHRASE() + "');";
        String getIdPAnnotation = "SELECT max(id_CONTENT_ANNOTATION) from CONTENT_ANNOTATION";

        PgConnect conn = PgConnect.getInstance();
        try {
           // ResultSet rs_tp = conn.executeQuery(testPAnnotation);
           // if (rs_tp.next()) {
             //   i = rs_tp.getInt("id_content_annotation");
           // } else {
                conn.executeInsert(insertPAnnotation);
                ResultSet rs = conn.executeQuery(getIdPAnnotation);
                while (rs.next()) {
                    i = rs.getInt("max");
                }
            //}
        } catch (SQLException ex) {
            ex.getStackTrace();
        }
        conn.Close();
        return i;
    }
//    public static void main(String[] args){
//        DAO_ContentAnnotation dc = DAO_ContentAnnotation.getInstance();
//        Photo_ContentAnnotation ca = new Photo_ContentAnnotation();
//        ca.setPHRASE("Phrase_ContentAnnotation");
//        int i = dc.insertAnnotation(ca);
//        System.out.println(i);
//    }
}
